package com.kgc.scd.controller;

import com.kgc.scd.bean.IndexPic;
import com.kgc.scd.contant.CommonConstant;
import com.kgc.scd.mapper.IndexPicMapper;
import com.kgc.scd.service.IndexPicService;
import com.kgc.scd.service.IndexSearchFeign;
import com.kgc.scd.util.MyUtils;
import com.kgc.scd.util.RedisUtils;
import com.kgc.scd.util.SensitiveWordFilterUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @program: com.kgc.scd.controller
 * @author: HarryXzy
 * @create: 2022-06-16 12:25
 * @description:
 **/
@Slf4j
@RestController
public class IndexPicController {

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private IndexPicService indexPicService;
    @Autowired(required = false)
    private IndexPicMapper indexPicMapper;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /***
    * @author: HarryXzy
    * @date: 2022/6/16 13:25
    * @return: java.util.Map<java.lang.String,java.lang.Object>
    * @description: 获取所有的首页轮播图
    */
    @PostMapping("/allIndexPic")
    public Map<String, Object> indexSearch(){
        HashMap<String, Object> map = new HashMap<>();
        //List<Object> indexPic = redisUtils.lGet("IndexPic",0,-1);
        Set<Object> indexPic = redisTemplate.opsForZSet().range("IndexPic", 0, -1);
        if(indexPic.size() == 0){
            // 去数据库查
            List<IndexPic> indexPics = indexPicService.getAllIndexPic();
            // 存入redis中
            for (IndexPic pic : indexPics) {
                redisTemplate.opsForZSet().add("IndexPic",pic,pic.getSortId());
                //redisUtils.lSet("IndexPic",pic);
            }
            map.put("code", CommonConstant.UNIFY_RETURN_SUCCESS_CODE);
            map.put("msg",CommonConstant.UNIFY_RETURN_SUCCESS_MSG);
            map.put("data",indexPics);
            return map;
        }
        map.put("code", CommonConstant.UNIFY_RETURN_SUCCESS_CODE);
        map.put("msg",CommonConstant.UNIFY_RETURN_SUCCESS_MSG);
        map.put("data",indexPic);
        return map;
    }

    /**
    * @author: HarryXzy
    * @date: 2022/6/16 13:38
    * @return: java.util.Map<java.lang.String,java.lang.Object>
    * @description: 添加轮播图
    */
    @PostMapping("/addIndexSearch")
    public Map<String, Object> addIndexSearch(@RequestBody IndexPic indexPic){
        HashMap<String, Object> map = new HashMap<>();
        if(indexPicService.checkPic(indexPic)){
            // 存在完全一样的轮播图
            map.put("code", "501");
            map.put("msg","已存在完全一致及顺序相同的轮播图");
            map.put("data","");
            return map;
        }
        // 检查轮播图是否超过10
        if(indexPicService.checkNum()){

        }
        indexPic.setIndexpicId(generateIndexPicId());
        indexPicService.addPic(indexPic);
        redisTemplate.opsForZSet().add("IndexPic",indexPic,indexPic.getSortId());
        Set<Object> allIndexPic = redisTemplate.opsForZSet().range("IndexPic", 0, -1);
        map.put("code", CommonConstant.UNIFY_RETURN_SUCCESS_CODE);
        map.put("msg","轮播图添加成功");
        map.put("data",allIndexPic);
        return map;
    }



    public String generateIndexPicId(){
        // 生成用户ID的组成规则：系统标识+时间字符串+随机序列号
        return "P"
                + LocalDateTime.now().format(DateTimeFormatter.ofPattern(CommonConstant.TIME_PATTERN_DAY))
                + MyUtils.generateNonceStrUseUUID(4);
    }

    /***
     * @author: HarryXzy
     * @date: 2022/6/16 13:38
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     * @description: 删除轮播图
     */
    @PostMapping("/deleteIndexSearch")
    public Map<String, Object> deleteIndexSearch(@RequestParam String indexPicId){
        HashMap<String, Object> map = new HashMap<>();
        IndexPic pic = indexPicService.getPicById(indexPicId);
        redisTemplate.opsForZSet().remove("IndexPic",pic);
        indexPicService.deletePicById(indexPicId);
        Set<Object> allIndexPic = redisTemplate.opsForZSet().range("IndexPic", 0, -1);
        map.put("code", CommonConstant.UNIFY_RETURN_SUCCESS_CODE);
        map.put("msg",CommonConstant.UNIFY_RETURN_SUCCESS_MSG);
        map.put("data",allIndexPic);
        return map;
    }


    /***
     * @author: HarryXzy
     * @date: 2022/6/16 13:38
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     * @description: 修改轮播图的顺序
     */
    @PostMapping("/changePicSort")
    public Map<String, Object> changePicSort(@RequestParam String indexPicId,@RequestParam Integer sort){
        HashMap<String, Object> map = new HashMap<>();
        IndexPic oldIndexPic = indexPicMapper.selectById(indexPicId);
        if(indexPicService.changeSort(indexPicId,sort)){
            // redis中删除原来的
            redisTemplate.opsForZSet().remove("IndexPic",oldIndexPic);
            IndexPic indexPic = indexPicMapper.selectById(indexPicId);
            redisTemplate.opsForZSet().add("IndexPic",indexPic,indexPic.getSortId());
            map.put("code", CommonConstant.UNIFY_RETURN_SUCCESS_CODE);
            map.put("msg",CommonConstant.UNIFY_RETURN_SUCCESS_MSG);
            map.put("data","");
            return map;
        }
        map.put("code", "501");
        map.put("msg","轮播图编号不存在");
        map.put("data","");
        return map;
    }


      
}
